﻿// Copyright (c) Microsoft Corporation.
// This sample code is provided "as is" without warranty of any kind. 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Dynamics.IntegrationFramework.Transform;

namespace FMInc.Transforms
{
    public class TxtToXmlTransform : ITransform
    {
        public void Transform(System.IO.Stream input, System.IO.Stream output, string configuration)
        {
            var messagesBuilder = new StringBuilder();
            using (var reader = new StreamReader(input))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    line = line.Trim();

                    var document = EmbedUtil.GetEmbeddedFileText("EmbedRecallDocument.xml");
                    document = string.Format(document, line);
                    var message = EmbedUtil.GetEmbeddedFileText("EmbedMessage.xml");
                    message = string.Format(message, document);

                    messagesBuilder.AppendLine(message);
                }
            }

            var messageSet = EmbedUtil.GetEmbeddedFileText("EmbedMessageSet.xml");
            messageSet = string.Format(messageSet, messagesBuilder.ToString());

            WriteTextToStream(output, messageSet);
        }

        public static void WriteTextToStream(Stream stream, string text)
        {
            using (var streamWriter = new StreamWriter(stream))
            {
                streamWriter.Write(text);
            }
        }
    }
}































